
 /**
 * UserManagerImpl.java.java.
 * User: BrianChen Date: 2012-6-5 下午05:00:05
 *
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.collapsar.core.security.service.impl;

import java.io.File;

import com.collapsar.core.orm.page.Page;
import com.collapsar.core.security.dao.IUserDao;
import com.collapsar.core.security.dao.IUserInfoDao;
import com.collapsar.core.security.model.User;
import com.collapsar.core.security.model.UserInfo;
import com.collapsar.core.security.service.IUserManager;
import com.collapsar.core.service.BaseUserAwareManager;

/**
 * <pre>
 * REVISIONS: 
 * Version 	Date 		Author 			Description
 * ------------------------------------------------------------------- 
 * 1.0 		2012-6-5 	BrianChen 	1. Created this class. 
 * </pre>
 * 
 * @author BrianChen
 * @date 2012-6-5 下午05:00:05
 * @version 1.0
 * @see
 * 
 */
public class UserManagerImpl extends BaseUserAwareManager implements IUserManager{
	private IUserDao userDao = null;
	private IUserInfoDao userInfoDao = null;


	@Override
	public UserInfo queryUserInfoByUserAccount(String userAccount) {
		return this.userInfoDao.getUserInfoByUserAccount(userAccount);
	}

	@Override
	public UserInfo queryUserInfo(Long userInfoId) {
		return this.userInfoDao.getById(userInfoId);
	}
	
	@Override
	public boolean modifyUserInfo(UserInfo userInfo, File uploadFile){
		//保存文件
		
		//更新信息
		
		userInfo.setUserPhoto(uploadFile.getName());
		this.userInfoDao.storeUserInfo(userInfo);
		
		return true;
	}
	
	@Override
	public boolean deleteUser(Long userId) {
		//Long userIds[] = {userId};
		//this.userDao.deleteAllUsers(userIds);
		//假删除
		User dbUser = this.userDao.getUser(userId);
		dbUser.setEnable(false);
		this.userDao.storeUser(dbUser);
		return true;
	}
	
	@Override
	public Page<User> queryAllUsers(int offset, int pageSize){
		logger.info("####come to userManager offset="+offset+", pageSize="+pageSize);
		return this.userDao.findAllUsers(offset, pageSize);
	}

	@Override
	public boolean regist(User user) {
		logger.info("####"+user);
		User dbUser = this.userDao.getUserByAccount(user.getAccount());
		if(dbUser != null){
			this.setMessage(R_USER_EXIST);
			return false;
		}else{
			//初始化用户详情
			UserInfo userInfo = new UserInfo(user.getAccount(), user.getPasswd(), user.getEmail());
			this.userInfoDao.storeUserInfo(userInfo);
			//关联上userInfo
			user.setUserInfo(userInfo);
			this.userDao.storeUser(user);
			return true;
		}
	}
	
	@Override
	public boolean login(String account, String passwd) {
		logger.info("####account="+account+", passwd="+passwd);
		User dbUser = this.userDao.getUserByAccount(account);
		if(dbUser == null){
			this.setMessage(L_USER_NOT_EXIST);
			return false;
		}else if(!dbUser.getPasswd().equals(passwd)){
			this.setMessage(L_USER_PWD_WRONG);
			return false;
		}else if(!dbUser.isEnable()){
			this.setMessage(L_USER_DISABLE);
			return false;
		}else{
			return true;
		}
	}


	@Override
	public void deleteUser(User user) {
		this.userDao.deleteUser(user);
	}

	@Override
	public void deleteAllUsers(Long[] userIds) {
		this.userDao.deleteAllUsers(userIds);
	}

	@Override
	public User loadUser(Long userId) {
		return this.loadUser(userId);
	}

//	@Override
//	public User loadUserWithGroupsAndRoles(Long userId) {
//		User user = this.userDao.loadUserWithGroupsAndRoles(userId);
//		return user;
//	}

	@Override
	public User getUserByAccount(String userAccount) {
		return this.userDao.getUserByAccount(userAccount);
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public IUserInfoDao getUserInfoDao() {
		return userInfoDao;
	}

	public void setUserInfoDao(IUserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}






}

